package com.qf.dao.impl;

import com.qf.dao.OrderDao;
import com.qf.entity.Order;
import com.qf.utils.DataSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @author wgy
 * @version V1.0
 * @project gp12_xiaomi
 * @package com.qf.dao.impl
 * @description: ${TODO}
 * @date 2022/6/27 9:28
 */
public class OrderDaoImpl implements OrderDao {
    @Override
    public void insert(Order order) {
        QueryRunner qr=new QueryRunner();
        Connection conn= DataSourceUtils.getConnection();
        Object[] params={order.getId(),order.getUid(),order.getMoney(),order.getStatus(),order.getTime(),order.getAid()};
        try {
            qr.update(conn, "insert into tb_order values(?,?,?,?,?,?);",params);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {//释放资源
            DataSourceUtils.closeAll(null, null, conn);
        }
    }

    @Override
    public void updateStatus(String oid, String status) {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        try {
            qr.update("update tb_order set status=? where id=?",status,oid );
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Order> selectByUid(int uid) {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        try {
            return qr.query("select * from tb_order where uid=?", new BeanListHandler<>(Order.class),uid);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public Order selectByOid(String oid) {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        try {
            return qr.query("select * from tb_order where id=?", new BeanHandler<>(Order.class),oid);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Order> selectAll() {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        try {
            return qr.query("select o.*,u.username from tb_order as o inner join tb_user as u on o.uid=u.id", new BeanListHandler<>(Order.class));
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }

    }

    @Override
    public List<Order> selectByWhere(String where, List<Object> params) {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        try {
            return qr.query("select o.*,u.username from tb_order as o inner join tb_user as u on o.uid=u.id "+ where, new BeanListHandler<>(Order.class),params.toArray());
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}
